package com.yuewen.permissioncheck

import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.activity_permission_check.*
import java.text.SimpleDateFormat
import java.util.*

class PermissionCheckActivity : AppCompatActivity() {

  lateinit var mAdapter: MyAdapter

  private val mReceiver = object : BroadcastReceiver() {
    @SuppressLint("NotifyDataSetChanged")
    override fun onReceive(context: Context?, intent: Intent?) {
      mAdapter.notifyDataSetChanged()
    }
  }

  @SuppressLint("NotifyDataSetChanged")
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_permission_check)
    clear.setOnClickListener {
      PermissionCheckManager.list.clear()
      mAdapter.notifyDataSetChanged()
    }
    recyclerView.layoutManager = LinearLayoutManager(this)
    mAdapter = MyAdapter(this)
    recyclerView.adapter = mAdapter

    LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, IntentFilter(
      PermissionCheckManager.PERMISSION_CHECK_RECEIVER
    ))
  }

  class MyHolder(val containerView: View): RecyclerView.ViewHolder(containerView) {
    val tvLine1 = containerView.findViewById<TextView>(R.id.tvLine1)
    val tvLine2 = containerView.findViewById<TextView>(R.id.tvLine2)
    val ivLeft = containerView.findViewById<ImageView>(R.id.ivLeft)
  }
  class MyAdapter(private val context: Context): RecyclerView.Adapter<MyHolder>() {

    private var mExpandIndex = -1

    @SuppressLint("InflateParams")
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {
      return MyHolder(LayoutInflater.from(context).inflate(R.layout.recycler_view_item, parent, false))
    }

    @SuppressLint("SimpleDateFormat", "SetTextI18n", "NotifyDataSetChanged")
    override fun onBindViewHolder(holder: MyHolder, position: Int) {
      val item = PermissionCheckManager.list[position]
      val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
      holder.tvLine1.text = "${simpleDateFormat.format(Date(item.time))} 正在获取:${item.permissionDescription}"
      val i = position
      holder.containerView.setOnClickListener {
        if (mExpandIndex == i) {
          mExpandIndex = -1
        } else {
          mExpandIndex = i
        }
        notifyDataSetChanged()
      }
      if (mExpandIndex == position) {
        holder.tvLine2.visibility = View.VISIBLE
        holder.tvLine2.text = item.stackTraceStr
        holder.ivLeft.setImageResource(android.R.drawable.arrow_up_float)
      } else {
        holder.ivLeft.setImageResource(android.R.drawable.arrow_down_float)
        holder.tvLine2.visibility = View.GONE
      }
    }

    override fun getItemCount(): Int {
      return PermissionCheckManager.list.size
    }
  }
}
